package com.stud.system.controller.system;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.stud.common.annotation.Log;
import com.stud.common.core.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.core.page.TableDataInfo;
import com.stud.common.enums.BusinessType;
import com.stud.common.utils.DictUtils;
import com.stud.common.utils.poi.ExcelUtil;
import com.stud.system.domain.Area;
import com.stud.system.dto.AreaDTO;
import com.stud.system.service.AreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 区域信息Controller
 *
 * @author Stud
 * @date 2022-09-02
 */
@Tag(name = "区域信息")
@RestController
@RequestMapping("/system/area")
public class AreaController extends BaseController {
    @Autowired
    private AreaService areaService;

    @Operation(summary = "查询区域信息列表")
    @GetMapping("/list")
    public TableDataInfo<Area> list(Area area) {
        QueryWrapper<Area> wrapper = new QueryWrapper(area);
        List<Area> list = areaService.list(wrapper);
        return getDataTable(list);
    }

    @Schema(description = "区域信息列表选择器")
    @GetMapping("/selector")
    public Result<List<Area>> selector(Area area) {
        QueryWrapper<Area> wrapper = new QueryWrapper(area);
        wrapper.select("id", "parent_id", "area_name")
                .orderByDesc("sort");
        List<Area> list = areaService.list(wrapper);
        return Result.ok(list);
    }

    @Schema(description = "导出区域信息列表")
    @PreAuthorize("@ss.hasPermi('system:area:export')")
    @Log(title = "区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Area area) {
        Wrapper<Area> wrapper = new QueryWrapper(area);
        List<Area> list = areaService.list(wrapper);
        DictUtils.dict(list);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        util.exportExcel(response, list, "区域信息数据");
    }

    @Schema(description = "获取区域信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:area:query')")
    @GetMapping(value = "/{id}")
    public Result<Area> getInfo(@PathVariable("id") Long id) {
        return Result.ok(areaService.getById(id));
    }

    @Schema(description = "根据区域编码查询区域详细信息")
    @GetMapping(value = "getInfoByAreaNo")
    public Result<Area> getInfoByAreaNo(@RequestParam String areaNo) {
        LambdaQueryWrapper<Area> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Area::getAreaNo, areaNo);
        return Result.ok(areaService.getOne(wrapper));
    }

    @Schema(description = "新增区域信息")
    @PreAuthorize("@ss.hasPermi('system:area:add')")
    @Log(title = "区域信息", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Boolean> add(@Validated @RequestBody Area area) {
        return Result.ok(areaService.save(area));
    }

    @Schema(description = "修改区域信息")
    @PreAuthorize("@ss.hasPermi('system:area:edit')")
    @Log(title = "区域信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Boolean> edit(@Validated @RequestBody Area area) {
        return Result.ok(areaService.update(area));
    }

    @Schema(description = "删除区域信息")
    @PreAuthorize("@ss.hasPermi('system:area:remove')")
    @Log(title = "区域信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Boolean> remove(@PathVariable Long[] ids) {
        return Result.ok(areaService.removeByIds(ids));
    }


    @Schema(description = "递归获取所有下级区域")
    @GetMapping("/recurseChildren")
    public Result<List<AreaDTO>> recurseChildren(Long areaId) {
        return Result.ok(areaService.recurseChildren(areaId));
    }

}
